package cn.xiyue.production.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import cn.xiyue.production.R;
import cn.xiyue.production.base.CallableActivity;

public class AirActivity extends CallableActivity implements View.OnClickListener {

    private ImageView iv_snow;
    private ImageView iv_sun;
    private Button btn_snow;
    private Button btn_sun;
    private ImageButton btn_switch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_air);
        initView();
    }

    private void initView() {
        iv_snow = (ImageView) findViewById(R.id.iv_snow);
        iv_sun = (ImageView) findViewById(R.id.iv_sum);
        btn_snow = (Button) findViewById(R.id.btn_snow);
        btn_sun = (Button) findViewById(R.id.btn_sun);
        btn_switch = (ImageButton) findViewById(R.id.btn_switch);

        btn_snow.setOnClickListener(this);
        btn_sun.setOnClickListener(this);
        btn_switch.setOnClickListener(this);

        async(requestApi.getFactoryInfo(1),value->{
            int acSwitch = value.getData().get(0).getAcSwitch();
            setAcSwitch(acSwitch);
        });
    }

    @Override
    public void onClick(View v) {
        int acSwitch;
        String msg;
        switch (v.getId()) {
            case R.id.btn_snow:
                acSwitch = 1;
                msg = "开启冷风成功";
                break;
            case R.id.btn_sun:
                acSwitch = 2;
                msg = "开启暖风成功";
                break;
            case R.id.btn_switch:
                acSwitch = 0;
                msg = "关闭电源成功";
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + v.getId());
        }
        async(requestApi.updateAcSwitch(1,acSwitch),value->{
            int i = value.getData().get(0).getAcSwitch();
            setAcSwitch(i);
            showToast(msg);
        });
    }

    private void setAcSwitch(int acSwitch) {
        switch (acSwitch){
            case 0:
                iv_snow.setImageResource(R.drawable.snow_off);
                iv_sun.setImageResource(R.drawable.sun_off);
                btn_switch.setImageResource(R.drawable.switch_off);
                break;
            case 1:
                iv_snow.setImageResource(R.drawable.snow_on);
                iv_sun.setImageResource(R.drawable.sun_off);
                btn_switch.setImageResource(R.drawable.switch_on);
                break;
            case 2:
                iv_snow.setImageResource(R.drawable.snow_off);
                iv_sun.setImageResource(R.drawable.sun_on);
                btn_switch.setImageResource(R.drawable.switch_on);
                break;
        }
    }
}